import { useRoute, useRouter } from 'vue-router'

type IQuery = {
    [property: string]: any
    step: number
}

export function useStep() {
    const router = useRouter()
    const route = useRoute()
    const steps: any = [
        {
            desc: '阅读并同意平台协议',
            title: '同意协议',
        },
        {
            desc: '填写详细机构信息',
            title: '填写资料',
        },
        {
            desc: '选择认证方式',
            title: '认证方式',
        },
        {
            desc: '提交认证',
            title: '认证完成',
        },
    ]

    function stepMove(query?: IQuery) {
        if (!query?.step && query?.step !== 0) {
            router.push('/')
            return
        }

        const str = ['agreement', 'profile', 'mode', 'finish', 'result'][query.step]

        router.push(
            {
                path: `/authentication/${str}`,
                query,
            },
        )
    }

    function initStep(data: any) {
        // 第二步回到第一步 结果页点重现认证
        if (route.query?.form === '1' || route.query?.form === '4') {
            return
        }

        if (data) {
            const { auditStatus, authType, isDraft } = data
            // 存在提交记录 协议回显
            window.sessionStorage.setItem('AGREEMENT_CHECK', '1')

            if (auditStatus === 2) {
                stepMove({ auditStatus, step: 4 })
                return
            }

            // 存在草稿 直接跳转表单回显
            if (isDraft === 1) {
                if (authType !== undefined && authType !== null) {
                    // 去到认证方式回显
                    stepMove({ step: 2 })
                }
                else {
                    // 去到填写资料回显
                    stepMove({ step: 1 })
                }
            }
            else {
                // 非草稿 去到审核结果页面
                stepMove({ auditStatus, step: 4 })
            }
        }
    }

    return { initStep, stepMove, steps }
}
